package com.wocao.commom.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 接口返回数据格式
 *
 * @author scott
 * @email jeecgos@163.com
 * @date 2019年1月19日
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final int SUCCESS_CODE = 200;

    private static final int ERROR_CODE = 500;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String message = "操作成功！";

    /**
     * 返回代码
     */
    private Integer code = 200;

    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public static <T> Result<T> SUEECSS(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(SUCCESS_CODE);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> SUEECSS(T data, String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(true);
        r.setCode(SUCCESS_CODE);
        r.setResult(data);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> ERROR(String msg) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(ERROR_CODE);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> ERROR(String msg, int code) {
        Result<T> r = new Result<T>();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(msg);
        return r;
    }

}
